<?php
namespace Index\Controller;
use Common\Common\Tree;
use Think\Controller;
header("Content-type: text/html; charset=utf-8");
class QuestionController extends Controller {
	//显示页面、如有插入操作就执行、读取话题
    public function IndexAction(){  
    	    $uid = $_SESSION["id"];
        	if(IS_POST){
                // var_dump($post=I('post.'));
                // exit();
    	    	$question_content = I('POST.question_content'); //标题
    	    	$category_id = I('POST.category_id');    //分类ID
    	    	$editorValue = I('POST.editorValue');	//内容
    	    	$topic_list = i('POST.topic_list');				//话题

    	    	$data['title'] = $question_content;
    	    	$data['cid'] = $category_id;
    	    	$data['centent'] = $editorValue;
    	    	$data['time'] = time();
    	    	$data['uid'] = $uid;
    	    	$data_m = M('question');
    	    	$res = $data_m->add($data); //添加问题表后会返回插入的ID
                if(!empty($topic_list)){    //如果话题不为空则进入
                    foreach ($topic_list as $key => $value) { //遍历下话题
                         $data_m=M();    
                         // var_dump($res);
                         $sql="insert into question_theme(qid,tid) value('$res','$value')"; //插入问题表的ID为$res是唯一的，但是$value值是多种的
                         $data_m->execute($sql);
                    }
                }
    	    	if($res){

                    //发表文章成功后需要将该动作保存到日志表中记录行为
                    $qid=$res;
                    $send_replay_question="发布了问题";
                    $time=date('Y-m-d H:i:s',time());
                    $data_m=M();
                    $sql="insert into log(uid,qid,send_replay_question,time) value('$uid','$qid','$send_replay_question','$time')";
                    $insert_res=$data_m->execute($sql);
                    //记录行为操作结束

                    if($insert_res){
                        $_SESSION['questionAddId'] = $res;
                        $this->success("发表成功",U('index/find/moreDetail',array('id'=>$res)),2);
                    }
    	    		
    	    	}else{
    	    		$this->error("操作失败",U('Index/Question/index'),2);
    	    	}
        	}else{

        		//数据库无限极分类
        		$sql = "select * from category";
    	    	$data_m = M();
    	    	$res = $data_m->query($sql);
                $obj = new Tree();//在公共文件中建一个类，用于数据的重组
                $res = $obj->reload($res);
    	    	// var_dump($res);
          //       exit();
    	    	

    	    	//数据库话题遍历
    	    	$sql_tip = "select * from theme";
    	    	$res_tip = $data_m->query($sql_tip);
    	    	// var_dump($res_tip);
    	    	// exit();
                $this->assign('categorys',$res);
    	    	$this->assign('res_tip',$res_tip);
    	    	$this->display();
        	} 	
    	
     }

     //当发布问题后跳转到该页面
    public function QuestionedAction(){  
    	// var_dump($_SESSION);
    	$uid = $_SESSION['questionAddId'];
    	$sql = "select * from question where id = $uid";
    	$data_m = M();
    	$res_questioned = $data_m->query($sql);
        // var_dump($res_questioned);
        // exit();
        $res_questioned[0]['time'] = time()-$res_questioned[0]['time'];//显示内容发布离现在过了多久
        if($res_questioned[0]['time']>(24*60*60)){
             $res_questioned[0]['time']=floor(($res_questioned[0]['time'])/(24*60*60))."天前";//转成天数
        }elseif($res_questioned[0]['time']>(60*60)){
             $res_questioned[0]['time']=floor(($res_questioned[0]['time'])/(60*60))."小时前";//转成小时
        }else{
            $res_questioned[0]['time']=floor(($res_questioned[0]['time'])/(60))."分钟前";//转成分钟
        }
        // var_dump($res_questioned);
        // exit();
    	$this->assign('res_questioned',$res_questioned);
    	$this->display();

    }

    //问题回复
    public function replayQuestionAction(){  
        $this->assign('res_questioned',$res_questioned);
        $this->display();

    }

    //点赞问题回复
    public function replayQuestionAgreeAction(){  
        $data_m=M();
        $uid=$_SESSION['id'];       
        $replayAgreeaid=I('post.aid');
        $replayuid=I('post.uid');
        //先查询表中该回复是否被这个用户点赞过，(点赞过为agree值为1) 错误理解
        // $status_agree_sql="select * from answer where id='$replayAgreeaid' and uid='$replayuid'";
        // $agree_replay_res=$data_m->query($status_agree_sql);
        // $agree_replay_res=current($agree_replay_res);
        // var_dump($agree_replay_res);
        // exit();
        //已点赞状态
        // if($agree_replay_res['agree']=="1"){
        //     //将agree状态转为0(取消点赞)
        //     $status_sql="update answer set agree='0' where id='$replayAgreeaid' and uid='$replayuid'";
        //     $agree_replay_status_res = $data_m->execute($status_sql);
        //     // var_dump($agree_replay_status_res);
        //     // exit();
        //     if($agree_replay_status_res){  
        //         $arr = array('success' => $agree_replay_status_res,'agree'=>0);
        //         echo json_encode($arr);//以json格式将其返回
        //     }else{
        //         $arr = array('error' => $agree_replay_status_res);
        //         echo json_encode($arr);
        //     }        
        //     //还未点赞状态    
        // }elseif($agree_replay_res['agree']=="0"){
        //     //将agree设置为1点赞状态
        //     $status_sql="update answer set agree='1' where id='$replayAgreeaid' and uid='$replayuid'";
        //      $agree_replay_status_res = $data_m->execute($status_sql);
        //     if($agree_replay_status_res){  
        //         $arr = array('success' => $agree_replay_status_res,'agree'=>1);
        //         echo json_encode($arr);
        //     }else{
        //         $arr = array('error' => $agree_replay_status_res);
        //         echo json_encode($arr);
        //     } 
        // }
        $answer_agree_sql="insert into answer_agree(aid,uid) value($replayAgreeaid,$uid)";
        $answer_agree_result=$data_m->execute($answer_agree_sql);
        var_dump($agree_replay_res);
        exit();
    }

     //百度编辑器插件
     public function ueditor(){
        $data = new \Org\Util\Ueditor();
        echo $data->output();
    }
     
    
}